package com.lcw.lostandfound.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @author lcw
 * @date 2020年10月9日10:24:44
 */
@Component
public class SendSMS {
    @Value("${SMS.accessKeyId}")
    private String accessKeyId;
    @Value("${SMS.secret}")
    private String secret;
    @Value("${SMS.SignName}")
    private String SignName;
    @Value("${SMS.TemplateCode}")
    private String TemplateCode;

    public String sendSMS(String tel) {
        String code = "";
        String TemplateParam="";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            code += String.valueOf(random.nextInt(10));
        }
        Map codeMap=new HashMap<>();
        codeMap.put("code",code);
        ObjectMapper obj=new ObjectMapper();
        try {
            TemplateParam = obj.writeValueAsString(codeMap);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(this.accessKeyId+this.secret+ tel+this.SignName+this.TemplateCode);
        String ret = sendMessage(this.accessKeyId, this.secret, tel, this.SignName, this.TemplateCode, TemplateParam);
        System.out.println(ret);
        return code;
    }

    private String sendMessage(String accessKeyId, String secret,String PhoneNumbers,String SignName,String TemplateCode,String TemplateParam) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("SignatureNonce", UUID.randomUUID().toString());
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", TemplateCode);
        request.putQueryParameter("TemplateParam", TemplateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
